#include "Directory.h"

FileSystem::Directory::Directory(const std::string &path)
    : FileSystem::Path(path)
{
}

std::vector<FileSystem::File> FileSystem::Directory::GetChildrenFiles() const
{
    std::vector<File> res;
    for (const auto &_fs_child : fs::directory_iterator(m_Path))
    {
        if (!fs::is_directory(_fs_child))
        {
            const std::string name = _fs_child.path().filename().string();
            res.push_back(File(name));
        }
    }

    return res;
}

std::vector<FileSystem::File> FileSystem::Directory::GetChildrenFilesSuffix(const std::string &suffix) const
{
    std::vector<File> res;
    for (const auto &_fs_child : fs::directory_iterator(m_Path))
    {
        if (!fs::is_directory(_fs_child))
        {
            const std::string name = _fs_child.path().string();

            FileSystem::File file(name);

            if (file.GetExtension() == suffix)
            {
                res.push_back(file);
            }
        }
    }

    return res;
}

std::vector<FileSystem::File> FileSystem::Directory::GetChildrenFilesSuffixIter(const std::string &suffix) const
{
    std::vector<File> res;
    for (const auto &_fs_child : fs::directory_iterator(m_Path))
    {
        if (!fs::is_directory(_fs_child))
        {
            const std::string name = _fs_child.path().string();

            FileSystem::File file(name);

            if (file.GetExtension() == suffix)
            {
                res.push_back(file);
            }
        }
        else
        {

            FileSystem::Directory childDir(_fs_child.path().string());
            LOGD("child path %s", childDir.GetRelativePath().c_str());
            std::vector<FileSystem::File> childFiles = childDir.GetChildrenFilesSuffixIter(suffix);
            res.insert(res.end(), childFiles.begin(), childFiles.end());
        }
    }

    return res;
}
